package com.ys.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.WebConstants;
import com.ys.extend.common.model.AjaxJson;
import com.ys.extend.utils.TimeUtil;
import com.ys.web.pojo.BaiduPromoteInfo;
import com.ys.web.pojo.SystemPermissonInfo;
import com.ys.web.security.SystemPermissionUtils;
import com.ys.web.service.OriginBaiduService;
import com.ys.web.service.PromotionPlanService;

/**
 * 推广数据
 * @author wdj
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/promoteDataController")
public class PromoteDataController {
	
	/**百度消费数据表(原始数据)-Service-接口*/
	@Autowired
	private OriginBaiduService originBaiduService;
	/**推广计划-Service-接口*/
	@Autowired
	private PromotionPlanService promotionPlanService;
	
	
	/**
	 * 经理角色-跳传“查看百度数据”页面（带公司id参数）
	 * @param request
	 * @return
	 */
	@RequestMapping(params="showBaidu_manager")
	public ModelAndView showBaidu_manager(HttpServletRequest request){
		String companyId = request.getParameter("companyId");		
		ModelAndView mav = new ModelAndView("promoteData/baiduDataManagerShow");
		if(companyId!=null && companyId.length()>0) {
			mav.addObject("companyId", companyId);			
		}
		else {
			try {
				throw new Exception("companyId参数为null");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		return mav;
	}
	
	/**
	 * SEM角色-跳传“查看百度数据”页面（带公司id参数）
	 * @param request
	 * @return
	 */
	@RequestMapping(params="showBaidu_sem")
	public ModelAndView showBaidu_sem(HttpServletRequest request){
		String companyId = request.getParameter("companyId");		
		ModelAndView mav = new ModelAndView("promoteData/baiduDataShow");
		if(companyId!=null && companyId.length()>0) {
			mav.addObject("companyId", companyId);			
		}
		else {
			try {
				throw new Exception("companyId参数为null");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		return mav;
	}
	
	
	/**
	 * 根据公司id查找对应推广计划
	 * @param companyId 账号id
	 * @param beginDate 开始日期
	 * @param endDate 结束日期 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params="baiduPromotePlanSelect")
	public List<Map<String,String>> getBaiduPromotePlanSelect(String companyId) {
		List<Map<String,String>> promoteList = null;

		if(companyId!=null && companyId.length()>0) {
			promoteList = promotionPlanService.getPlanToSelect(Integer.parseInt(companyId));		
		}
		else {
			try {
				throw new Exception("companyId参数为null");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		return promoteList;	
	}

	
	
	/**
	 * 获取百度消费数据，并根据页面边界值计算所属象限
	 * @param accountId 账号id
	 * @param begintime 开始时间  
	 * @param endtime 结束时间 
	 * @param quadrantIds 象限
	 * @param promotionPlan 推广计划
	 * @param promotionUnit 推广单元
	 * @param promotionKeyword 推广关键词
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params="getBaiduPromoteData")
	public List<BaiduPromoteInfo> getBaiduPromoteData(String companyId, String begintime,String endtime,Integer page, HttpServletRequest quest){
		List<BaiduPromoteInfo> baiduPromoteInfoList= new ArrayList<BaiduPromoteInfo>();
		String consumeRateMin_1 = quest.getParameter("consumeRateMin_1");
		String consumeRateMin_2 = quest.getParameter("consumeRateMin_2");
		String consumeRateMin_3 = quest.getParameter("consumeRateMin_3");
		String consumeRateMin_4 = quest.getParameter("consumeRateMin_4");
		String consumeRateMax_1 = quest.getParameter("consumeRateMax_1");
		String consumeRateMax_2 = quest.getParameter("consumeRateMax_2");
		String consumeRateMax_3 = quest.getParameter("consumeRateMax_3");
		String consumeRateMax_4 = quest.getParameter("consumeRateMax_4");

//		System.out.println("companyId--"+companyId);
//		System.out.println("begintime--"+begintime);
//		System.out.println("endtime--"+endtime);
		
		//暂时页面没传分页码
		Integer infopage = 1;
		if(page!=null){
			infopage = page;
		}
		//暂时页面没传分页码

		Integer companyId_par = null; 				
		if(companyId!=null && companyId.length()>0) {
			companyId_par =Integer.parseInt(companyId);
		}		
		Date dateBegin_par=null; 
		Date dateEnd_par=null;
		if(begintime!=null && endtime!=null && !begintime.isEmpty() && !endtime.isEmpty()) {
			dateBegin_par = TimeUtil.getFormatDate(begintime, "yyyy-MM-dd");
			dateEnd_par =TimeUtil.getFormatDate(endtime, "yyyy-MM-dd");			
		}
		if(!consumeRateMin_1.isEmpty() && !consumeRateMin_2.isEmpty() && !consumeRateMin_3.isEmpty() && !consumeRateMin_4.isEmpty() 
				&& !consumeRateMax_1.isEmpty() && !consumeRateMax_2.isEmpty() && !consumeRateMax_3.isEmpty() && !consumeRateMax_4.isEmpty()) {						
			//获取百度消费数据，并根据页面边界值计算所属象限，数据库默认每次返回1000条
			baiduPromoteInfoList = originBaiduService.getLimitBaiduInfoByCompanyId(companyId_par,dateBegin_par,dateEnd_par,infopage,
					Double.parseDouble(consumeRateMin_1),Double.parseDouble(consumeRateMin_2),Double.parseDouble(consumeRateMin_3),Double.parseDouble(consumeRateMin_4),
					Double.parseDouble(consumeRateMax_1),Double.parseDouble(consumeRateMax_2),Double.parseDouble(consumeRateMax_3),Double.parseDouble(consumeRateMax_4));						
		}		
		
		
		return baiduPromoteInfoList;
	}	
	
	/**
	 * 根据当前用户的角色，是否显示保存边界值按钮
	 * @return true：超管或者经理角色，显示；false：普通用户角色，不显示
	 */
	@ResponseBody
	@RequestMapping(params="getUerRole")
	public AjaxJson getUerRole(HttpSession session) {
		AjaxJson j = new AjaxJson();
		String username=(String)session.getAttribute("username");		
		Integer userId = (Integer)session.getAttribute("userId");
		String msg = "";
		try {
			if(username!=null&&userId!=null){
				SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
				if(systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						||systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)){				
					msg = "managerRole";
				}
				else {
					msg = "normalRole";
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		
		j.setMsg(msg);
		return j;
	}
	
}
